﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using Kojarskiy.Core;
using Kojarskiy.Models;

namespace Kojarskiy.Controllers
{
    public class PriceController : Controller
    {
        private KojarskiyDbEntities context = new KojarskiyDbEntities();

        [HttpGet]
        public FileResult GetExcelPriceList(string page)
        {
            var node = context.SiteMap.FirstOrDefault(x => x.Path == page);
            try
            {
                if (node != null)
                {
                    var fileName = "price-" + page + "-" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls";
                    var serverPath = Server.MapPath("~/Upload/Temp/" + fileName);
                    using (Stream outStream = new FileStream(serverPath, FileMode.OpenOrCreate))
                    {
                        var writer = new StreamWriter(outStream);
                        writer.Write(node.Content);
                        writer.Flush();
                        writer.Close();
                        return File(serverPath, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
                    }
                }
            }
            catch (Exception ex)
            {
                KojarskiyApplication.Log(KojarskiyApplication.ErrorType.Fatal, ex.Message, ex.StackTrace);
            }
            return null;
        }
        [HttpGet]
        public FileResult GetWordPriceList(string page)
        {
            var node = context.SiteMap.FirstOrDefault(x => x.Path == page);
            try
            {
                if (node != null)
                {
                    var fileName = "price-" + page + "-" + DateTime.Now.ToString("yyyy-MM-dd") + ".rtf";
                    var serverPath = Server.MapPath("~/Upload/Temp/" + fileName);
                    using (Stream outStream = new FileStream(serverPath, FileMode.OpenOrCreate))
                    {
                        var writer = new StreamWriter(outStream);
                        writer.Write(node.Content);
                        writer.Flush();
                        writer.Close();
                        return File(serverPath, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
                    }
                }
            }
            catch (Exception ex)
            {
                KojarskiyApplication.Log(KojarskiyApplication.ErrorType.Fatal, ex.Message, ex.StackTrace);
            }
            return null;
        }

        [HttpGet]
        public FileResult GetFullExcelPriceList()
        {
            var nodes = context.SiteMap.Where(x => x.Parent.Path == "price" && x.Parent.Culture == Localization.Current);
            try
            {
                var fileName = "price-full-" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls";
                var serverPath = Server.MapPath("~/Upload/Temp/" + fileName);
                using (Stream outStream = new FileStream(serverPath, FileMode.OpenOrCreate))
                {
                    var writer = new StreamWriter(outStream);
                    foreach (var node in nodes)
                    {
                        if (node != null)
                        {
                            writer.Write(node.Content);
                        }
                    }
                    writer.Flush();
                    writer.Close();
                }
                return File(serverPath, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
            }
            catch (Exception ex)
            {
                KojarskiyApplication.Log(KojarskiyApplication.ErrorType.Fatal, ex.Message, ex.StackTrace);
            }
            return null;
        }


        [HttpGet]
        public FileResult GetFullWordPriceList()
        {
            var nodes = context.SiteMap.Where(x => x.Parent.Path == "price" && x.Parent.Culture == Localization.Current);
            try
            {
                var fileName = "price-full-" + DateTime.Now.ToString("yyyy-MM-dd") + ".rtf";
                var serverPath = Server.MapPath("~/Upload/Temp/" + fileName);
                using (Stream outStream = new FileStream(serverPath, FileMode.OpenOrCreate))
                {
                    var writer = new StreamWriter(outStream);
                    foreach (var node in nodes)
                    {
                        if (node != null)
                        {                              
                            writer.Write(node.Content);
                        }                    
                    }
                    writer.Flush();
                    writer.Close();
                }
                return File(serverPath, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
            }
            catch (Exception ex)
            {
                KojarskiyApplication.Log(KojarskiyApplication.ErrorType.Fatal, ex.Message, ex.StackTrace);
            }
            return null;
        }
    }
}
